
package com.work.db.entity;

import java.io.Serializable;
import java.sql.Time;

import javax.persistence.*;
import javax.validation.constraints.NotNull;

import org.apache.commons.lang3.builder.*;

import com.work.util.AppUtils;

@Entity
@Table(name = "OVERTIME")
public class Overtime extends BaseEntity implements Serializable {

	@Transient
	private static final long serialVersionUID = -941029216794907265L;

	@NotNull
	@Column(name = "BEGIN", columnDefinition = "TIME")
	private Time begin;

	@NotNull
	@Column(name = "END", columnDefinition = "TIME")
	private Time end;

	@Column(name = "DESCRIPTION")
	private String description;

	@Column(name = "MINUTES")
	private int minutes;

	@ManyToOne
	private Project project;

	@ManyToOne
	private Rate rate;

	@ManyToOne(optional = false)
	private WorkDay workDay;

	public Overtime()
	{
		super();
	}

	public Time getBegin()
	{
		return begin;
	}

	public void setBegin(Time begin)
	{
		this.begin = begin;
	}

	public Time getEnd()
	{
		return end;
	}

	public void setEnd(Time end)
	{
		this.end = end;
	}

	public String getDescription()
	{
		return description;
	}

	public void setDescription(String description)
	{
		this.description = description;
	}

	public Project getProject()
	{
		return project;
	}

	public void setProject(Project project)
	{
		this.project = project;
	}

	public Rate getRate()
	{
		return rate;
	}

	public void setRate(Rate rate)
	{
		this.rate = rate;
	}

	public int getMinutes()
	{
		return minutes;
	}

	public void setMinutes(int minutes)
	{
		this.minutes = minutes;
	}

	public WorkDay getWorkDay()
	{
		return workDay;
	}

	public void setWorkDay(WorkDay workDay)
	{
		this.workDay = workDay;
	}

	public int calculateOverTime()
	{
		long overtime = getEnd().getTime() - getBegin().getTime();
		return (int) (overtime / AppUtils.MINUTE_MILLIS);
	}

	@Override
	public String toString()
	{
		return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
			.append("begin", getBegin())
			.append("end", getEnd())
			.append("description", getDescription())
			.append("minutes", getMinutes())
			.toString();
	}
}
